let router = require('../router/Router');

let Token = require('../token');
let ipUtil = require('../util/ipUtil');

let userService = require('../service/userService');
let clubMemberService = require('../service/ClubMemberService')

let redis = require('../dao/redis');

const {BusiError, SysError} = require('../exception/exception');

/**
 * 用户加入俱乐部
 */
router.post('/auth/user/joinClub', (req, resp) => {
    let uid = req.uid;
    let clubId = req.body.clubId;
    clubMemberService.joinClub(uid, clubId, req.ip, req.ua).then(res => {

        let member = { msgType:30, uid:uid, clubId:clubId, ownerUid:res.ownerUid };
        let s=JSON.stringify(member);
        //yield redis.publishUnion(s);
        redis.publishUnion(s);

        let result = {};
        result.code = 200;
        result.msg = "success";
        result.data = res;
        resp.send(result);
    }).catch(e => {
        let msg = "系统异常, 请稍后再试";
        if (e instanceof BusiError) {
            msg = e.message;
        }
        resp.send({ code: 500, msg: msg });
    });

});

/**
 * 新增俱乐部
 */
router.post('/auth/user/createClub', (req, resp) => {
    let uid = req.uid;
    let clubName = req.body.clubName;
    clubMemberService.createClub(uid, clubName, req.ip, req.ua).then(res => {
        let result = {};
        result.code = 200;
        result.msg = "success";
        result.data = res;
        resp.send(result);
    }).catch(e => {
        let msg = "系统异常, 请稍后再试";
        if (e instanceof BusiError) {
            msg = e.message;
        }
        resp.send({ code: 500, msg: msg });
    });
});

/**
 * 解散俱乐部
 */
router.post('/auth/user/dissolveClub', (req, resp) => {
 //  let uid = req.uid;
    let uid = req.body.uId;
    let clubId = req.body.clubId;
    clubMemberService.dissolveClub(uid, clubId, req.ip, req.ua).then(res => {
        let result = {};
        result.code = 200;
        result.msg = "success";
        result.data = res;
        resp.send(result);
    }).catch(e => {
        let msg = "系统异常, 请稍后再试";
        if (e instanceof BusiError) {
            msg = e.message;
        }
        resp.send({ code: 500, msg: msg });
    });
});

router.post('/auth/user/quitClub', (req, resp) => {
    let uid = req.uid;
    let clubId = req.body.clubId;
    clubMemberService.quitClub(uid, clubId, req.ip, req.ua).then(res => {
        let result = {};
        result.code = 200;
        result.msg = "success";
        result.data = res;
        resp.send(result);
    }).catch(e => {
        let msg = "系统异常, 请稍后再试";
        if (e instanceof BusiError) {
            msg = e.message;
        }
        resp.send({ code: 500, msg: msg });
    });
});

/**
 * 修改俱乐部的抽水设置
 */
router.post('/auth/user/club/updateRoyaltySetting', (req, resp) => {
    let uid = req.uid;
    let clubId = req.body.clubId;
    let data = req.body.data;
    clubMemberService.updateClubRoyaltySetting(uid, clubId, data, req.ip, req.ua).then(res => {
        let result = {};
        result.code = 200;
        result.msg = "success";
        result.data = res;
        resp.send(result);
    }).catch(e => {
        let msg = "系统异常, 请稍后再试";
        if (e instanceof BusiError) {
            msg = e.message;
        }
        resp.send({ code: 500, msg: msg });
    });
});

/**
 * 查询俱乐部信息
 */
router.post('/auth/user/queryClub', (req, resp) => {
    let uid = req.uid;
    let clubId = req.body.clubId;
    clubMemberService.getClubInfo(uid, clubId, req.ip, req.ua).then(res => {
        let result = {};
        result.code = 200;
        result.msg = "success";
        result.data = res;
        resp.send(result);
    }).catch(e => {
        let msg = "系统异常, 请稍后再试";
        if (e instanceof BusiError) {
            msg = e.message;
        }
        resp.send({ code: 500, msg: msg });
    });
});


/**
 * 保存俱乐部的抽水记录
 */
router.post('/game/club/royalty/record/save', (req, resp) => {
    let recordList = req.body.data;
    //add
    if(recordList[0].clubID === '5740934'){
        let result = {};
        result.code = 200;
        result.msg = "success";
        resp.send(result);
        return;
    }
    clubMemberService.saveClubRoyaltyRecord(recordList, req.ip, req.ua).then(res => {
        let result = {};
        result.code = 200;
        result.msg = "success";
        result.data = res;
        resp.send(result);
    }).catch(e => {
        console.log(e);
        let msg = "系统异常, 请稍后再试";
        if (e instanceof BusiError) {
            msg = e.message;
        }
        resp.send({ code: 500, msg: msg });
    });
});

/**
 * 按天获取俱乐部的抽水记录
 */
router.post('/auth/club/royalty/record/list', (req, resp) => {
    let date = req.body.date;
    let clubId = req.body.clubId;
    clubMemberService.getClubRoyaltyRecordList(date, clubId, req.ip, req.ua).then(res => {
        let result = {};
        result.code = 200;
        result.msg = "success";
        result.data = res;
        resp.send(result);
    }).catch(e => {
        let msg = "系统异常, 请稍后再试";
        if (e instanceof BusiError) {
            msg = e.message;
        }
        resp.send({ code: 500, msg: msg });
    });
});
